/**
 * 
 */
package ar.edu.utn.frsf.cidisi.ginet.generation.elementsestructure.elementstemplate;

import java.util.Map;

import org.eclipse.emf.common.util.EList;
import org.pnml.tools.epnk.pnmlcoremodel.AnnotationGraphics;
import org.pnml.tools.epnk.pnmlcoremodel.Coordinate;
import org.pnml.tools.epnk.pnmlcoremodel.Name;
import org.pnml.tools.epnk.pnmlcoremodel.PlaceNode;
import org.pnml.tools.epnk.pnmlcoremodel.PnmlcoremodelFactory;
import org.pnml.tools.epnk.pntypes.hlpng.pntd.hlpngdefinition.Declaration;

import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Arc;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Page;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Place;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.RefPlace;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Transition;
import ar.edu.utn.frsf.cidisi.ginet.generation.elementsestructure.NodeStructure;
import ar.edu.utn.frsf.cidisi.ginet.generation.elementsestructure.TreeInterface;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionPath;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocolElement;

/**
 * @author Neri
 *
 */
	
public class XorUpcolbpipStructure extends ChoreographyStructure {
	
	public XorUpcolbpipStructure(Integer i){		
		super();		
		//Name		
		AnnotationGraphics graphicsName = PnmlcoremodelFactory.eINSTANCE
				.createAnnotationGraphics();
		
		Name nameLabel = PnmlcoremodelFactory.eINSTANCE.createName();
		
		Coordinate coordinate2 = PnmlcoremodelFactory.eINSTANCE.createCoordinate();
		
		coordinate2.setX(-9);
		coordinate2.setY( -39);
		
		graphicsName.setOffset(coordinate2);
		
		nameLabel.setText("XOR" +i);
		nameLabel.setGraphics(graphicsName);
		
		this.abstractPage.setName(nameLabel);
	}
	
	@Override
	public void createPageStructure(NodeStructure node, TreeInterface tree, PlaceNode placeIn, PlaceNode placeOut, EList<Declaration> ginetDeclarations, Place termination){

		this.tree = tree;
		
		this.fillVarDeclAndNameSort(ginetDeclarations);
		
		Map<InteractionPath, EList<InteractionProtocolElement>> childrenCount = node.getChildrens();
		
		boolean whitRefPlaces = placeIn != null && placeOut != null;
		
		RefPlace refPlaceIn = null; 
		Place placeAndIn = null;
		if(whitRefPlaces){
			refPlaceIn = this.createRefPlace("InPlace",  1, 2, "InPlace", -4, -4, "1", placeIn);
		}else{
			placeAndIn = createPlace("InPlace", 1, 2, "InPlace", -4, -4, this.getVarDec("gi"), 6, 21, true, "1");
		}
		
		Transition transition1 = this.createTransition("xorit", 2, 2, "xorit", -4, -9, "1", "1", null);
		Arc arc1;
		if(whitRefPlaces){
			arc1 = this.createArc("", this.getVarDec("gi"), -37, -3, refPlaceIn, transition1);
		}else{
			arc1 = this.createArc("", this.getVarDec("gi"), -37, -3, placeAndIn, transition1);
		}

		Transition transition3 = this.createTransition("xorot", 6, 2, "ot", -9, -4, "1", "1", null);
		
		RefPlace refPlaceOut = null; 
		Place placeAndOut = null;
		Arc arc6;
		if(whitRefPlaces){
			refPlaceOut = this.createRefPlace("OutPlace", 7, 2, "OutPlace", -4, -4, "1", placeOut);
		}else{
			placeAndOut = createPlace("OutPlace", 7, 2, "OutPlace", -4, -4, this.getVarDec("gi"), 6, 21, false, "1");
		}
		
		if(whitRefPlaces){
			arc6 = this.createArc("", this.getVarDec("gi"), -9, -19, transition3, refPlaceOut);
		}else{
			arc6 = this.createArc("", this.getVarDec("gi"), -9, -19, transition3, placeAndOut);
		}
		
		Place place1 = createPlace("xoripp", 3, 2, "xoripp", -4, -4, this.getVarDec("gi"), 6, 21, true, "1");
		
		Place place2 = createPlace("xoropp", 5, 2, "xoropp", -4, -4, this.getVarDec("gi"), 6, 21, false, "1");
		
		Arc arc2 = this.createArc("", this.getVarDec("gi"), -12, -8, transition1, place1);
		
		Arc arc5 = this.createArc("", this.getVarDec("gi"), -24, -3, place2, transition3);			
		
		int i = 1;
	
		for(InteractionPath inter: childrenCount.keySet()){
			
			if(inter.getElement().size() > 0){
				
				Page page = createPage("page" + 1,4,i, "page" + i, -9, -39);
				this.createInteractionProtocol(inter.getElement(), i, page, place1, place2, ginetDeclarations, termination);
				
				this.abstractPage.getObject().add(page);
			}else{
				
				Transition transition2 = this.createTransition("xorxor" + i, 4, i,"xor" + i, -9, -39, "1", "1", null);
				this.abstractPage.getObject().add(transition2);
				Arc arc3 = this.createArc("", this.getVarDec("gi"), -19, -6, place1, transition2);
				this.abstractPage.getObject().add(arc3);				
				Arc arc4 = this.createArc("", this.getVarDec("gi"), -9, -9, transition2, place2);
				this.abstractPage.getObject().add(arc4);
			}
			
			i++;
		}
		
		this.abstractPage.getObject().add(place1);
		this.abstractPage.getObject().add(place2);
		
		if(whitRefPlaces){
			this.abstractPage.getObject().add(refPlaceIn);
			this.abstractPage.getObject().add(refPlaceOut);
		}else{			
			this.abstractPage.getObject().add(placeAndIn);
			this.abstractPage.getObject().add(placeAndOut);
		}
		
		this.abstractPage.getObject().add(transition1);
		this.abstractPage.getObject().add(transition3);
		
		this.abstractPage.getObject().add(arc1);
		this.abstractPage.getObject().add(arc2);		
		this.abstractPage.getObject().add(arc5);
		this.abstractPage.getObject().add(arc6);		
	}

}